﻿package com.amcharts.axes
{
    import com.amcharts.geom.*;
    import com.amcharts.utils.*;
    import flash.display.*;
    import flash.events.*;
    import mx.collections.*;
    import mx.core.*;
    import mx.styles.*;

    public class AxisBase extends UIComponent
    {
        protected var _guideFillRenderer:Class;
        protected var _setGridCount:uint;
        protected var _orientation:String;
        protected var _maxLabelLength:Number;
        protected var _guides:Array;
        protected var _inside:Boolean;
        protected var _end:Number;
        protected var _fillColor:Number;
        protected var _textFormat:UITextFormat;
        protected var _grid:Sprite;
        protected var _axisWidth:Number;
        protected var _boundToAxis:ValueAxis;
        protected var _fillAlpha:Number;
        protected var _textSize:Number;
        protected var _dx:Number = 0;
        protected var _dy:Number = 0;
        protected var _boundToValue:Number = 0;
        protected var _gridAlpha:Number;
        protected var _gridColor:uint;
        protected var _previousCoord:Number;
        protected var _valuesContainer:Sprite;
        protected var _values:Sprite;
        protected var _labelsEnabled:Boolean;
        protected var _axisCreated:Boolean;
        protected var _height:Number;
        protected var _fills:Sprite;
        protected var _valueClickHandler:Function = null;
        protected var _labelInside:Boolean;
        protected var _title:String;
        protected var _titleTextSize:Number = 12;
        protected var _axisRenderer:Class;
        protected var _visibleAxisX:Number = 0;
        protected var _visibleAxisY:Number = 0;
        protected var _start:Number;
        protected var _ticks:Sprite;
        protected var _rotate:Boolean;
        protected var _axis:Sprite;
        protected var _position:String;
        protected var _tickLength:Number;
        protected var _titleAlignment:String = "center";
        protected var _labelRotation:Number;
        protected var _axisItemRenderer:Class;
        protected var _dataProvider:ArrayCollection;
        protected var _scrollbar:Boolean;
        protected var _ignoreAxisWidth:Boolean;
        protected var _dashLength:Number;
        protected var _axisTitle:IUITextField;
        protected var _offset:Number = 0;
        protected var _visibleAxisHeight:Number;
        protected var _counter:Number;
        protected var _axisAlpha:Number;
        protected var _visibleAxisWidth:Number;
        protected var _axisColor:uint;
        protected var _width:Number;
        protected var _gridThickness:Number;
        protected var _axisThickness:uint;
        protected var _maxLabelWidth:Number = 0;
        protected var _fillsMask:Rect;
        protected var _titleTextColor:Number;
        protected var _gridCount:uint;
        protected var _dataChanged:Boolean;
        static const VERSION:String = "1.8.3.1";
        private static var stylesInited:Boolean = initStyles();

        public function AxisBase()
        {
            this.includeInLayout = false;
            _dataProvider = new ArrayCollection();
            _guides = new Array();
            _valuesContainer = this;
            this.addEventListener("show", toggleVisibility);
            this.addEventListener("hide", toggleVisibility);
            return;
        }// end function

        public function get axisCreated() : Boolean
        {
            return _axisCreated;
        }// end function

        public function set axisRenderer(param1:Class) : void
        {
            _axisRenderer = param1;
            return;
        }// end function

        public function set visibleAxisX(param1:Number) : void
        {
            _visibleAxisX = param1;
            return;
        }// end function

        public function set guideFillRenderer(param1:Class) : void
        {
            _guideFillRenderer = param1;
            return;
        }// end function

        public function get guides() : Array
        {
            return _guides;
        }// end function

        protected function updateData() : void
        {
            return;
        }// end function

        public function get inside() : Boolean
        {
            return _inside;
        }// end function

        public function set visibleAxisY(param1:Number) : void
        {
            _visibleAxisY = param1;
            return;
        }// end function

        public function set dx(param1:Number) : void
        {
            _dx = param1;
            invalidateDisplayList();
            return;
        }// end function

        public function get values() : Sprite
        {
            return _values;
        }// end function

        public function get position() : String
        {
            return _position;
        }// end function

        public function get previousCoord() : Number
        {
            return _previousCoord;
        }// end function

        public function clean() : void
        {
            Utils.removeChildren(_axis);
            Utils.removeChildren(_grid);
            Utils.removeChildren(_fills);
            Utils.removeChildren(_values);
            Utils.removeChildren(_ticks);
            return;
        }// end function

        public function get fills() : Sprite
        {
            return _fills;
        }// end function

        protected function adjustTitlePosition() : void
        {
            if (_axisTitle)
            {
                if (_orientation == "horizontal")
                {
                    if (_position == "bottom")
                    {
                        _axisTitle.y = _maxLabelWidth - 5;
                    }
                    else
                    {
                        _axisTitle.y = -_maxLabelWidth - _titleTextSize;
                    }
                }
                else if (_position == "left")
                {
                    _axisTitle.x = -_maxLabelWidth - _titleTextSize + _offset;
                }
                else
                {
                    _axisTitle.x = _maxLabelWidth - _titleTextSize + 5 - _offset;
                }
            }
            return;
        }// end function

        public function createTextField() : IUITextField
        {
            var _loc_1:* = IUITextField(createInFontContext(UITextField));
            return _loc_1;
        }// end function

        override protected function createChildren() : void
        {
            super.createChildren();
            _fills = new Sprite();
            addChild(_fills);
            _fillsMask = new Rect(1, 1, [0], [1]);
            addChild(_fillsMask);
            _fills.mask = _fillsMask;
            _grid = new Sprite();
            addChild(_grid);
            _axis = new Sprite();
            addChild(_axis);
            _values = new Sprite();
            if (_valueClickHandler == null)
            {
                _values.mouseChildren = false;
                _values.mouseEnabled = false;
            }
            _ticks = new Sprite();
            _ticks.mouseChildren = false;
            _ticks.mouseEnabled = false;
            return;
        }// end function

        public function get grid() : Sprite
        {
            return _grid;
        }// end function

        public function set dy(param1:Number) : void
        {
            _dy = param1;
            invalidateDisplayList();
            return;
        }// end function

        public function get rotate() : Boolean
        {
            return _rotate;
        }// end function

        public function get fillsMask() : Sprite
        {
            return _fillsMask;
        }// end function

        public function set gridCountReal(param1:Number) : void
        {
            if (_setGridCount != param1)
            {
                _setGridCount = param1;
                _gridCount = param1;
                _dataChanged = true;
            }
            return;
        }// end function

        public function fixPosition() : void
        {
            if (_orientation == "horizontal")
            {
                if (!_position)
                {
                    _position = "bottom";
                }
                if (_position == "left")
                {
                    _position = "bottom";
                }
                if (_position == "right")
                {
                    _position = "top";
                }
            }
            else
            {
                if (!_position)
                {
                    _position = "left";
                }
                if (_position == "bottom")
                {
                    _position = "left";
                }
                if (_position == "top")
                {
                    _position = "right";
                }
            }
            return;
        }// end function

        public function set position(param1:String) : void
        {
            _position = param1;
            invalidateDisplayList();
            return;
        }// end function

        public function get visibleAxisWidth() : Number
        {
            return _visibleAxisWidth;
        }// end function

        public function get visibleAxisHeight() : Number
        {
            return _visibleAxisHeight;
        }// end function

        public function set offset(param1:Number) : void
        {
            _offset = param1;
            invalidateDisplayList();
            return;
        }// end function

        public function set valueClickHandler(param1:Function) : void
        {
            _valueClickHandler = param1;
            if (param1 != null)
            {
                _values.mouseChildren = true;
                _values.mouseEnabled = true;
            }
            return;
        }// end function

        public function set visibleAxisHeight(param1:Number) : void
        {
            _visibleAxisHeight = param1;
            return;
        }// end function

        public function set scrollbar(param1:Boolean) : void
        {
            _scrollbar = param1;
            return;
        }// end function

        public function get titleTextSize() : Number
        {
            return _titleTextSize;
        }// end function

        public function set dataProvider(param1:ArrayCollection) : void
        {
            _dataProvider = param1;
            _dataChanged = true;
            _axisCreated = false;
            invalidateDisplayList();
            return;
        }// end function

        public function set previousCoord(param1:Number) : void
        {
            _previousCoord = param1;
            return;
        }// end function

        public function get dx() : Number
        {
            return _dx;
        }// end function

        public function get ticks() : Sprite
        {
            return _ticks;
        }// end function

        public function set axisItemRenderer(param1:Class) : void
        {
            _axisItemRenderer = param1;
            return;
        }// end function

        public function get dy() : Number
        {
            return _dy;
        }// end function

        public function get visibleAxisX() : Number
        {
            return _visibleAxisX;
        }// end function

        public function get axis() : Sprite
        {
            return _axis;
        }// end function

        public function set titleTextSize(param1:Number) : void
        {
            _titleTextSize = param1;
            return;
        }// end function

        public function set visibleAxisWidth(param1:Number) : void
        {
            _visibleAxisWidth = param1;
            return;
        }// end function

        public function get offset() : Number
        {
            return _offset;
        }// end function

        public function get title() : String
        {
            return _title;
        }// end function

        public function set title(param1:String) : void
        {
            _title = param1;
            return;
        }// end function

        public function get titleAlignment() : String
        {
            return _titleAlignment;
        }// end function

        protected function toggleVisibility(event:Event) : void
        {
            invalidateDisplayList();
            return;
        }// end function

        public function set titleTextColor(param1:Number) : void
        {
            _titleTextColor = param1;
            return;
        }// end function

        public function get maxLabelWidth() : Number
        {
            return _maxLabelWidth + _offset + 10;
        }// end function

        public function get visibleAxisY() : Number
        {
            return _visibleAxisY;
        }// end function

        public function get dataProvider() : ArrayCollection
        {
            return _dataProvider;
        }// end function

        protected function createAxis() : void
        {
            if (_valuesContainer.contains(_values))
            {
                _valuesContainer.removeChild(_values);
            }
            _valuesContainer.addChild(_values);
            if (_valuesContainer.contains(_ticks))
            {
                _valuesContainer.removeChild(_ticks);
            }
            _valuesContainer.addChild(_ticks);
            _fillsMask.width = this.width;
            _fillsMask.height = this.height;
            _fillsMask.x = _dx;
            _fillsMask.y = _dy;
            _fills.x = _dx;
            _fills.y = _dy;
            _counter = 0;
            if (!_gridCount)
            {
                _gridCount = getStyle("gridCount");
            }
            var _loc_1:* = new _axisRenderer(this);
            _axisWidth = _loc_1.axisWidth;
            if (_loc_1.hasOwnProperty("axisTitle"))
            {
                _axisTitle = _loc_1.axisTitle;
            }
            if (this.getStyle("autoGridCount"))
            {
                if (_orientation == "vertical")
                {
                    _gridCount = _axisWidth / 35;
                    if (_gridCount < 3)
                    {
                        _gridCount = 3;
                    }
                }
                else
                {
                    _gridCount = _axisWidth / 75;
                }
                updateData();
            }
            return;
        }// end function

        public function get titleTextColor() : Number
        {
            return _titleTextColor;
        }// end function

        public function get valueClickHandler() : Function
        {
            return _valueClickHandler;
        }// end function

        public function get orientation() : String
        {
            return _orientation;
        }// end function

        override public function styleChanged(param1:String) : void
        {
            super.styleChanged(param1);
            if (param1 == "gridCount")
            {
                _gridCount = getStyle("gridCount");
                invalidateDisplayList();
                return;
            }
            return;
        }// end function

        public function zoom(param1:Number, param2:Number) : void
        {
            _dataChanged = true;
            _start = param1;
            _end = param2;
            invalidateDisplayList();
            return;
        }// end function

        public function set titleAlignment(param1:String) : void
        {
            _titleAlignment = param1;
            return;
        }// end function

        public function removeGuide(param1:Guide) : void
        {
            var _loc_2:* = _guides.length;
            var _loc_3:Number = 0;
            while (_loc_3 < _loc_2)
            {
                
                if (_guides[_loc_3] == param1)
                {
                    _guides.splice(_loc_3, 1);
                }
                _loc_3 = _loc_3 + 1;
            }
            invalidateDisplayList();
            return;
        }// end function

        public function addGuide(param1:Guide) : Guide
        {
            _guides.push(param1);
            invalidateDisplayList();
            return param1;
        }// end function

        public function set ignoreAxisWidth(param1:Boolean) : void
        {
            _ignoreAxisWidth = param1;
            invalidateDisplayList();
            return;
        }// end function

        public function set maxLabelLength(param1:Number) : void
        {
            _maxLabelLength = param1;
            invalidateDisplayList();
            return;
        }// end function

        public function get ignoreAxisWidth() : Boolean
        {
            return _ignoreAxisWidth;
        }// end function

        public function set guides(param1:Array) : void
        {
            var _loc_2:uint = 0;
            var _loc_3:Guide = null;
            _guides = new Array();
            if (param1)
            {
                _loc_2 = 0;
                while (_loc_2 < param1.length)
                {
                    
                    _loc_3 = param1[_loc_2];
                    _guides.push(_loc_3);
                    _loc_2 = _loc_2 + 1;
                }
            }
            invalidateDisplayList();
            return;
        }// end function

        public function get maxLabelLength() : Number
        {
            return _maxLabelLength;
        }// end function

        override protected function updateDisplayList(param1:Number, param2:Number) : void
        {
            super.updateDisplayList(param1, param2);
            clean();
            if (_dataProvider.length > 0)
            {
                createAxis();
            }
            return;
        }// end function

        public function set counter(param1:Number) : void
        {
            _counter = param1;
            return;
        }// end function

        public function destroy() : void
        {
            clean();
            if (_valuesContainer.contains(_values))
            {
                _valuesContainer.removeChild(_values);
            }
            if (_valuesContainer.contains(_ticks))
            {
                _valuesContainer.removeChild(_ticks);
            }
            _valuesContainer = this;
            Utils.removeChildren(this);
            return;
        }// end function

        public function get counter() : Number
        {
            return _counter;
        }// end function

        public function set valuesContainer(param1:Sprite) : void
        {
            _valuesContainer = param1;
            invalidateDisplayList();
            return;
        }// end function

        public function set inside(param1:Boolean) : void
        {
            _inside = param1;
            invalidateDisplayList();
            return;
        }// end function

        private static function initStyles() : Boolean
        {
            var styleDeclaration:* = StyleManager.getStyleDeclaration("AxisBase");
            if (!styleDeclaration)
            {
                styleDeclaration = new CSSStyleDeclaration();
            }
            styleDeclaration.defaultFactory = function () : void
            {
                this.axisThickness = 1;
                this.axisColor = 0;
                this.axisAlpha = 1;
                this.tickLength = 5;
                this.gridCount = 5;
                this.gridAlpha = 0.2;
                this.gridThickness = 0;
                this.gridColor = 0;
                this.dashLength = 0;
                this.labelFrequency = 1;
                this.showFirstLabel = true;
                this.showLastLabel = true;
                this.fillColor = 16777215;
                this.fillAlpha = 0;
                this.labelsEnabled = true;
                this.labelRotation = 0;
                this.labelBackgroundAlpha = 0;
                this.labelBackgroundColor = 14342874;
                this.radarCategoriesEnabled = true;
                this.axisTitleOffset = 10;
                this.gridType = "polygons";
                this.autoGridCount = false;
                this.valueRollOverColor = 13369344;
                return;
            }// end function
            ;
            StyleManager.setStyleDeclaration("AxisBase", styleDeclaration, false);
            return true;
        }// end function

    }
}
